<!--
 * @Description:业务系统
-->
<template>
  <div class="registered-container allData appMainWarp">
    <el-form :inline="true" :model="searchKey" class="demo-form-inline">
      <el-form-item>
        <el-button :type="0 == quaflat ? 'primary':''" @click="getlisttable(0)">按天显示</el-button>
        <el-button :type="1 == quaflat ? 'primary':''" @click="getlisttable(1)">按月显示</el-button>
        <el-button :type="2 == quaflat ? 'primary':''" @click="getlisttable(2)">按年显示</el-button>
        <el-button :type="3 == quaflat ? 'primary':''" @click="getlisttable(3)">汇总显示</el-button>
      </el-form-item>

      <el-form-item label="交易时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        />
      </el-form-item>

      <el-form-item label="院区:">
        <el-select v-model="searchKey.hospital_area_no" placeholder="请选择">
          <el-option
            v-for="item in searchKey.Hospital"
            :key="item.area_code"
            :label="item.area_name"
            :value="item.area_code"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="选择支付场景:">
        <el-select v-model="searchKey.system_no" multiple placeholder="请选择支付场景" clearable>
          <el-option
            v-for="item in searchKey.System"
            :key="item.pay_scene_no"
            :label="item.pay_scene_name"
            :value="item.pay_scene_no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="对账业务:">
        <el-select v-model="searchKey.reconciliation" placeholder="请选择" @change="onLoad">
          <el-option
            v-for="item in searchKey.reconciliationList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-download" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>
    <template>
      <div class="alldata">
        <div class="kioskdata back">
          <div class="title">
            <div class="line" />
            <div class="title-text">自助机统计情况统计</div>
          </div>
          <div class="charts">
            <el-row :gutter="20" class="panel-group">
              <el-col :xs="24" :sm="24" :lg="12" :xl="12">
                <div class="chart-wrapper">
                  <!-- <p class="box-between line-warp-title">
                    <span>自助机平均每日使用情况时段图 </span>
                  </p> -->
                  <bar-chart :chart-data="barChartItem" />
                </div>
              </el-col>
              <el-col :xs="24" :sm="24" :lg="12" :xl="12">
                <div class="chart-wrapper">
                  <!-- <p class="box-between line-warp-title">
                    <span>自助机平均一周每日使用分布图 </span>
                  </p> -->
                  <line-chart :chart-data="lineChartItem" />
                </div>
              </el-col>
            </el-row>
            <!-- <bar-kiosk-chart v-if="kioskdata.item" :items-info="kioskdata" /> -->
            <!-- <div class="chart-two">
              <line-chart-two-group :items-info="kioskdata" />
            </div> -->
          </div>

          <template>
            <el-table
              ref="table"
              v-loading="page.tableLoading"
              max-height="500px"
              center
              class="table"
              :data="searchData"
              stripe
              border
            >
              <el-table-column label="时间" align="center" prop="time">
                <template slot-scope="scope">{{ scope.row.time||'0' }}</template>
              </el-table-column>
              <el-table-column
                v-if="searchKey.reconciliation !== '3'"
                :key="1"
                prop="self_income"
                label="支付场景收入"
                align="center"
              >
                <template slot-scope="scope">{{ scope.row.self_income||'0' }}</template>
              </el-table-column>
              <el-table-column
                v-if="searchKey.reconciliation !== '2'"
                :key="2"
                prop="his_income"
                label="HIS系统收入"
                align="center"
              >
                <template slot-scope="scope">{{ scope.row.his_income	||'0' }}</template>
              </el-table-column>
              <el-table-column
                v-if="searchKey.reconciliation !== '1'"
                :key="3"
                prop="three_income"
                label="支付系统收入"
                align="center"
              >
                <template slot-scope="scope">{{ scope.row.three_income	||'0' }}</template>
              </el-table-column>
              <el-table-column
                v-if="searchKey.reconciliation == '1' || searchKey.reconciliation == '4'"
                :key="4"
                label="支付场景—HIS系统"
                align="center"
                prop="self_his_income"
              >
                <template slot-scope="scope">
                  <div
                    :class="[ scope.row.self_his_fee === '0.00'? 'abnormal':'normal']"
                    @click="jumpClick(scope.row,1,scope.row.self_his_fee)"
                  >{{ scope.row.self_his_income ||'0' }}</div>
                </template>
              </el-table-column>

              <el-table-column
                v-if="searchKey.reconciliation == '2' || searchKey.reconciliation == '4'"
                :key="5"
                label="支付场景—支付系统"
                align="center"
                prop="self_three_income"
              >
                <template slot-scope="scope">
                  <div
                    :class="[scope.row.self_three_fee ==='0.00'?'abnormal':'normal']"
                    @click="jumpClick(scope.row,2,scope.row.self_three_fee)"
                  >{{ scope.row.self_three_income ||'0' }}</div>
                </template>
              </el-table-column>

              <el-table-column
                v-if="searchKey.reconciliation == '3' || searchKey.reconciliation == '4'"
                :key="6"
                label="HIS系统—支付系统"
                align="center"
                prop="his_three_income"
              >
                <template slot-scope="scope">
                  <div
                    :class="[ scope.row.his_three_fee==='0.00'?'abnormal':'normal']"
                    @click="jumpClick(scope.row,3,scope.row.his_three_fee)"
                  >{{ scope.row.his_three_income||'0' }}</div>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </div>
      </div>
    </template>
    <pagination
      :hidden="page.totalPage<=1"
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />

  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 分页
// import BarKioskChart from './components/BarKioskChart'
import BarChart from './components/components-group/BarList'
import LineChart from './components/components-group/LineList'
// import LineChartTwoGroup from './components/LineChartTwoGroup'
// import TableGroup from './components/TableGroup'
import { getConfigType, generalaccount } from '@/api/transactRecords/index'
import { getCompareType } from '@/utils/auth'

export default {
  name: 'AllData',
  components: {
    Pagination,
    // BarKioskChart,
    BarChart,
    LineChart
    // ,
    // LineChartTwoGroup,
    // TableGroup
  },
  data() {
    return {

      devData: {
        // title: '医院列表',
        item: null,
        page: {
          tableLoading: false,
          currentPage: 1,
          pageSize: 20,
          totalRow: 0,
          totalPage: 0
        }
      },
      kioskdata: {
        item: null,
        chart: []
      },
      quaflat: '0', // 按钮默认
      date: 'day', // 按天月年
      searchKey: {
        time: '',
        hospital_area_no: '', // 选择院区
        Hospital: [],
        system_no: '', // 选择平台
        System: [],
        reconciliation: '', // 对账业务
        reconciliationList: []
      },

      searchData: [],
      page: {
        tableLoading: false,
        currentPage: 1,
        pageSize: 20,
        totalRow: 0,
        totalPage: 0
      },
      minDate: '',
      maxDate: '',
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.minDate = minDate
          this.maxDate = maxDate
        },
        disabledDate: (time) => { // 查询时间跨度为365天
          if (this.minDate) {
            var range = 365 * 24 * 3600 * 1000
            return time.getTime() > Date.now() || time.getTime() > (this.minDate.getTime() + range) || time.getTime() < (this.minDate.getTime() - range)
          }
          return time.getTime() > Date.now()
        }
      },
      // pickerOptions: {
      //   // 日期不能点击
      //   disabledDate(time) {
      //     return time.getTime() > Date.now() || time.getTime() === Date.now() || time.getTime() < new Date().setFullYear((new Date().getFullYear() - 1))
      //   }
      // },
      barChartItem: {
        bus_type_name: [],
        total_num: []
      },
      lineChartItem: {
        bus_type_name: [],
        total_fee: []
      }
    }
  },
  watch: {
    'searchKey.time': {
      deep: true,
      handler(val) {
        if (val == null) {
          this.minDate = null
        }
      }
    }
  },
  created() {
    this.searchKey.time = this.utils.Foryest()
    this.searchKey.reconciliation = getCompareType()
  },
  mounted() {
    this.getConfig()
    this.onLoad()
  },
  methods: {
    // 系统配置搜索
    getConfig() {
      var that = this
      var params = {}
      getConfigType(params)
        .then(res => {
          that.searchKey.Hospital = res.data.hospital
          that.searchKey.System = res.data.scene
          that.searchKey.reconciliationList = res.data.compare
        })
        .catch(() => {})
    },
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.type = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.starttime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.starttime = ''
        params.endtime = ''
      }
      params.areano = that.searchKey.hospital_area_no
      params.payscene = that.searchKey.system_no.toString()
      params.comparetype = that.searchKey.reconciliation.toString()
      that.page.tableLoading = true
      generalaccount(params)
        .then((res) => {
          that.page.tableLoading = false
          that.searchData = res.data.result.table_data
          // that.page.currentPage = res.data.page
          that.page.totalPage = res.data.pages
          that.page.totalRow = parseInt(res.data.count)
          if (res.data.result.bar_data.length > 0) {
            var datas = {}
            Object.keys(res.data.result.bar_data[0]).forEach((item) => {
              datas[item] = res.data.result.bar_data.map((option) => option[item])
            })
            that.kioskdata.item = datas
            this.barChartItem.bus_type_name = this.kioskdata.item.bus_type_name
            this.barChartItem.total_num = this.kioskdata.item.total_num
            this.lineChartItem.bus_type_name = this.kioskdata.item.bus_type_name
            this.lineChartItem.total_fee = this.kioskdata.item.total_fee
          } else {
            this.barChartItem.bus_type_name = []
            this.barChartItem.total_num = []
            this.lineChartItem.bus_type_name = []
            this.lineChartItem.total_fee = []
          }
        })
        .catch(() => {
          that.page.tableLoading = false
        })
    },
    // 导出
    onExport() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.type = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.starttime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.starttime = ''
        params.endtime = ''
      }
      params.areano = that.searchKey.hospital_area_no
      params.payscene = that.searchKey.system_no.toString()
      params.comparetype = that.searchKey.reconciliation.toString()
      var viewUrl = `/cms/generalaccount?&page=${params.page}&limit=${params.limit}&type=${
        params.type
      }&areano=${
        params.areano
      }&starttime=${
        params.starttime
      }&endtime=${params.endtime}&comparetype=${params.comparetype}&export=${1}`
      window.location.href = process.env.VUE_APP_BASE_API + viewUrl
    },
    jumpClick(item, type, val) {
      return
      // if (val === 0 || val === '0.00') {
      //   return
      // } else {
      //   this.$router.push({
      //     path: '/generaLedger/totalRevenue/totalRevenueDetail/index',
      //     query: {
      //       date: item.date,
      //       comparetype: type
      //     }
      //   })
      // }
    },
    getlisttable(flat) {
      this.quaflat = flat
      if (this.quaflat === 0) {
        this.date = 'day'
        this.onLoad()
      } else if (this.quaflat === 1) {
        this.date = 'month'
        this.onLoad()
      } else if (this.quaflat === 2) {
        this.date = 'year'
        this.onLoad()
      } else {
        this.date = 'all'
        this.onLoad()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.allData {
  position: relative;
  width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
    .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }
  .kioskdata {
    margin: 20px 0;
  }
  .back {
    padding: 20px 40px;
    background: #fafafb;
    border-radius: 10px;
  }
}
.abnormal{
  color: #717685;
}
.normal{
  color: red;
}
.chart-wrapper{
    background: #fff;
    padding: 20px;
    margin: 20px 0;
}
</style>
